package com.telchina.workorder.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.telchina.common.core.domain.entity.SysOrg;
import com.telchina.common.utils.DateUtils;
import com.telchina.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.telchina.common.annotation.Log;
import com.telchina.common.core.controller.BaseController;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.enums.BusinessType;
import com.telchina.workorder.domain.WoReturnVisit;
import com.telchina.workorder.service.IWoReturnVisitService;
import com.telchina.common.utils.poi.ExcelUtil;
import com.telchina.common.core.page.TableDataInfo;

/**
 * 回访单Controller
 *
 * @author konghao
 * @date 2023-02-21
 */
@RestController
@RequestMapping("/workorder/woReturnVisit")
public class WoReturnVisitController extends BaseController {
    @Autowired
    private IWoReturnVisitService woReturnVisitService;

    /**
     * 分页查询回访单列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:visit:list')")
    @GetMapping("/list")
    public AjaxResult list(WoReturnVisit woReturnVisit) {
        startPage();
//        IPage<WoReturnVisit> list = woReturnVisitService.selectList(startPage(), woReturnVisit);
        List<WoReturnVisit> list = woReturnVisitService.selectListAll(woReturnVisit);
        return getDataTable(list);
    }

    /**
     * 查询回访单列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:visit:list')")
    @GetMapping("/list/all")
    public AjaxResult listAll(WoReturnVisit woReturnVisit) {
        List<WoReturnVisit> list = woReturnVisitService.selectListAll(woReturnVisit);
        return AjaxResult.success(list);
    }

    /**
     * 导出回访单列表
     */
//    @PreAuthorize("@ss.hasPermi('workorder:visit:export')")
    @Log(title = "回访单" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WoReturnVisit woReturnVisit) {
        List<WoReturnVisit> list = woReturnVisitService.selectListAll(woReturnVisit);
        ExcelUtil<WoReturnVisit> util = new ExcelUtil<WoReturnVisit>(WoReturnVisit.class);
        return util.exportExcel(list, "visit");
    }

    /**
     * 获取回访单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('workorder:visit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(woReturnVisitService.selectById(id));
    }

    /**
     * 新增回访单
     */
//    @PreAuthorize("@ss.hasPermi('workorder:visit:add')")
    @Log(title = "回访单" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WoReturnVisit woReturnVisit) {
        return woReturnVisitService.insert(woReturnVisit);
    }

    /**
     * 修改回访单
     */
//    @PreAuthorize("@ss.hasPermi('workorder:visit:edit')")
    @Log(title = "回访单" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WoReturnVisit woReturnVisit) {
        return toAjax(woReturnVisitService.update(woReturnVisit));
    }

    /**
     * 删除回访单
     */
//    @PreAuthorize("@ss.hasPermi('workorder:visit:remove')")
    @Log(title = "回访单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(woReturnVisitService.deleteByIds(ids));
    }

    /**
     * 删除回访单
     */
//    @PreAuthorize("@ss.hasPermi('workorder:visit:add')")
    @GetMapping("/getInitReturnVisit")
    public AjaxResult getInitReturnVisit() {
        Map<String, Object> result = new HashMap<>();
        // 获取登录人所在部门信息
        result.put("serviceTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
        result.put("returnVisitNo", DateUtils.parseDateToStr("yyMMdd", new Date()));
        result.put("userName", SecurityUtils.getLoginUser().getUsername());
        return AjaxResult.success(result);
    }

}
